using Godot;
using System.Collections.Generic;

public class HpBar : Control
{
    PackedScene HeartScene = GD.Load<PackedScene>("res://Tscn/Heart.tscn");
    public int last = 0;
    public int now = 0;
    List<Heart> Hears = new List<Heart>();
    public override void _Ready()
    {

    }
    private void Clear()
    {
        for (int i = 0; i < Hears.Count; i++)
        {
            RemoveChild(Hears[i]);
            Hears[i].QueueFree();
            Hears.RemoveAt(i);
        }
    }
    private void Create(int num)
    {
        for (int i = 0; i < num; i++)
        {
            Heart h = HeartScene.Instance<Heart>();
            this.AddChild(h);
            Hears.Add(h);
            h.SetPosition(new Vector2(i * 16, 0));
        }
    }
    public void SetHealth(int health, int max)
    {
        if (health != last)
        {
            Clear();
            Create(health);
        }

        last = now;
    }
}
